<?php 
class ControllerNewsNews extends Controller {
	public function index() {  
    	$this->language->load('news/news');
		
		$this->load->model('catalog/news');
		//$this->load->model('tool/seo_url');
		$this->load->model('tool/image');
                $this->load->model('catalog/newscategory');    
		$this->document->breadcrumbs = array();
		
      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTP_SERVER . 'index.php?route=common/home',
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);		
      	
      	if (isset($this->error['name'])) {
    			$this->data['error_name'] = $this->error['name'];
			} else {
				$this->data['error_name'] = '';
			}
			
			if (isset($this->error['email'])) {
    			$this->data['error_email'] = $this->error['email'];
			} else {
				$this->data['error_email'] = '';
			}
			
			if (isset($this->error['comment'])) {
    			$this->data['error_comment'] = $this->error['comment'];
			} else {
				$this->data['error_comment'] = '';
			}
			
			if (isset($this->error['captcha'])) {
				$this->data['error_captcha'] = $this->error['captcha'];
			} else {
				$this->data['error_captcha'] = '';
			}	
		
		
		
		// Related News start
		if (isset($this->request->get['news_id'])) {
			$this->data['related_newss'] = array();
		
			foreach ($this->model_catalog_news->getRelatedNews($this->request->get['news_id']) as $result) {
      		$this->data['related_newss'][] = array(
        		'title' => $result['title'],      
        		'image' => $this->model_tool_image->resize($result['image'], 100, 100),
	    		'href'  => $this->url->link('news/news', 'news_id=' . $result['news_id'])								
      		);
    		}
    	}
		// Related News end
		
		if (isset($this->request->get['news_id'])) {
			$news_id = $this->request->get['news_id'];
		} else {
			$news_id = 0;
		}
		
		$news_info = $this->model_catalog_news->getNews($news_id);
   		
		if ($news_info) {
	  		
	  		// Count Read start
			$this->data['new_read_counter_value'] = $news_info['count_read']+1;
			$this->model_catalog_news->updateNewsReadCounter($this->request->get['news_id'], $this->data['new_read_counter_value']);
			// Count Read end
			
			$this->data['date_added'] = $news_info['date_added'];
			$this->data['date_modified'] = $news_info['date_modified'];
			$this->data['count_read'] = $news_info['count_read']+1;
			$this->data['description'] = html_entity_decode($news_info['description']);  
			$this->data['allow_comment'] = $news_info['allow_comment']; 
			$this->data['comment_permission'] = $news_info['comment_permission']; 
			$this->data['comment_need_approval'] = $news_info['comment_need_approval'];
			
			$this->data['comment_total'] = $this->model_catalog_news->getTotalCommentsByNewsId($this->request->get['news_id']);
	  		
	  		$this->document->setTitle = $news_info['title']; 
	  		
	  		$this->document->setKeywords = $news_info['meta_keyword'];
			
			$this->document->setDescription = $news_info['meta_description'];

      		$this->document->breadcrumbs[] = array(
        		'href'      => HTTP_SERVER . 'index.php?route=news/news&news_id=' . $this->request->get['news_id'],
        		'text'      => $news_info['title'],
        		'separator' => $this->language->get('text_separator')
      		);		
						
      	// Send Comment start
			
			if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
				
				$pattern = '/^[A-Z0-9._%-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i';
				
				if((strlen(utf8_decode($this->request->post['name'])) < 3)  || (strlen(utf8_decode($this->request->post['name'])) > 64)){
					$this->data['error_messages'] = $this->language->get('error_name');
				}
				
    			elseif (!preg_match($pattern, $this->request->post['email'])) {
      			$this->data['error_messages'] = $this->language->get('error_email');
    			}
		
				elseif ((strlen(utf8_decode($this->request->post['comment'])) < 5) || (strlen(utf8_decode($this->request->post['comment'])) > 1000)) {
					$this->data['error_messages'] = $this->language->get('error_comment');
				}

	 			elseif (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
					$this->data['error_messages'] = $this->language->get('error_captcha');
				}
				
				else{
					if($this->data['comment_need_approval'] == '1'){
						$comment_status = '0';
						$comment_success_message = $this->language->get('success_messages_approval');
					}else{
						$comment_status = '1';
						$comment_success_message = $this->language->get('success_messages');
					}
					$this->model_catalog_news->addComment($this->request->get['news_id'], $this->request->post['name'], $this->request->post['email'], $this->request->post['comment'], $comment_status);	
					$this->data['success_messages'] = $comment_success_message;
				}			
			
			}			
			// Send Comment end
      	
      	$this->data['heading_title'] = $news_info['title'];
      	
      	$this->data['text_updated_on'] = $this->language->get('text_updated_on');
      	$this->data['text_posted_on'] = $this->language->get('text_posted_on');
      	$this->data['text_read'] = $this->language->get('text_read');
      	$this->data['text_times'] = $this->language->get('text_times');
      	$this->data['text_comment'] = $this->language->get('text_comment');
      	$this->data['text_write_comment'] = $this->language->get('text_write_comment');
      	$this->data['text_note'] = $this->language->get('text_note');
        $this->data['text_no_comment'] = $this->language->get('text_no_comment');
        $this->data['text_comments'] = $this->language->get('text_comments');
        $this->data['text_comment_must_logged'] = $this->language->get('text_comment_must_logged');
        $this->data['text_related_news'] = $this->language->get('text_related_news');
      	
      	$this->data['entry_name'] = $this->language->get('entry_name');
      	$this->data['entry_email'] = $this->language->get('entry_email');
      	$this->data['entry_comment'] = $this->language->get('entry_comment');
      	$this->data['entry_captcha'] = $this->language->get('entry_captcha');
      		      		
      	$this->data['button_continue'] = $this->language->get('button_continue');
						     												
			$this->data['logged'] = $this->customer->isLogged();
			$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
			
			$this->data['action'] = HTTP_SERVER . 'index.php?route=news/news&news_id=' . $this->request->get['news_id'] . '#comment_form';
			
			if (isset($this->request->post['name'])) {
				$this->data['name'] = $this->request->post['name'];
			} else {
				$this->data['name'] = '';
			}
			
			if (isset($this->request->post['email'])) {
				$this->data['email'] = $this->request->post['email'];
			} else {
				$this->data['email'] = '';
			}
			
			if (isset($this->request->post['comment'])) {
				$this->data['comment'] = $this->request->post['comment'];
			} else {
				$this->data['comment'] = '';
			}
			
			// Comment Validation end
			
			// Get Comment start
			
			if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} else {
				$page = 1;
			}  
		
			$this->data['comments'] = array();
			
			$results = $this->model_catalog_news->getCommentsByNewsId($this->request->get['news_id'], ($page - 1) * $this->config->get('news_setting_comments_per_page'), $this->config->get('news_setting_comments_per_page'));
      		
			foreach ($results as $result) {
        		$this->data['comments'][] = array(
        			'name'     => $result['name'],
					'email'     => $result['email'],
					'comment'       => strip_tags($result['comment']),
        			'date_added' => $result['date_added']
        		);
      	}			
		
			$comment_total = $this->model_catalog_news->getTotalCommentsByNewsId($this->request->get['news_id']);
			
			$pagination = new Pagination();
			$pagination->total = $comment_total;
			$pagination->page = $page;
			$pagination->limit = $this->config->get('news_setting_comments_per_page'); 
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = HTTP_SERVER . 'index.php?route=news/news&news_id=' . $this->request->get['news_id'] . '&page={page}#comment_area';
			
			$this->data['pagination'] = $pagination->render();

			// Get Comment end
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/news.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/news/news.tpl';
			} else {
				$this->template = 'default/template/news/news.tpl';
			}
			
			$this->children = array(
				'common/column_right',
				'common/footer',
				'common/column_left',
				'common/header'
			);		
			
	  		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    	} else {
      		$this->document->breadcrumbs[] = array(
        		'href'      => HTTP_SERVER . 'index.php?route=news/news',
        		'text'      => $this->language->get('text_news'),
        		'separator' => $this->language->get('text_separator')
      		);
				
	  		$this->document->setTitle = $this->language->get('text_news');
			
      		$this->data['heading_title'] = $this->language->get('text_news');

      		$this->data['text_news'] = $this->language->get('text_news');
      		$this->data['text_news_not_found'] = $this->language->get('text_news_not_found');
      		$this->data['text_updated_on'] = $this->language->get('text_updated_on');
      		$this->data['text_posted_on'] = $this->language->get('text_posted_on');
      	   $this->data['text_read'] = $this->language->get('text_read');
      	   $this->data['text_times'] = $this->language->get('text_times');
         	$this->data['text_read_more'] = $this->language->get('text_read_more');
         	$this->data['text_comments'] = $this->language->get('text_comments');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
      		
      		// News All		
      		if (isset($this->request->get['page'])) {
					$page = $this->request->get['page'];
				} else {
					$page = 1;
				}  
				
		$this->data['newss'] = array();
		if (isset($this->request->get['path'])) {
			$path = '';
		
			$parts = explode('_', (string)$this->request->get['path']);
		
			foreach ($parts as $path_id) {
				if (!$path) {
					$path = $path_id;
				} else {
					$path .= '_' . $path_id;
				}
									
				$category_info = $this->model_catalog_newscategory->getnews_category($path_id);
				
				if ($category_info) {
	       			$this->data['breadcrumbs'][] = array(
   	    				'text'      => $category_info['name'],
						'href'      => $this->url->link('news/news', 'path=' . $path),
        				'separator' => $this->language->get('text_separator')
        			);
				}
			}		
		
			$category_id = array_pop($parts);
		} else {
			$category_id = 0;
		}
		$results = $this->model_catalog_news->getNewss(($page - 1) * $this->config->get('news_setting_news_per_page'), $this->config->get('news_setting_news_per_page'),$category_id);
		foreach ($results as $result) {
      		$this->data['newss'][] = array(
        		'title' => $result['title'],
        		'date_added' => $result['date_added'],
        		'date_modified' => $result['date_modified'],
        		'description' => html_entity_decode($result['description']),
        		'short_description' => html_entity_decode($result['short_description']),
        		'count_read' => $result['count_read'],
        		'image' => $this->model_tool_image->resize($result['image'], 100, 100),
	    		'href'  => $this->url->link('news/news', 'news_id=' . $result['news_id']),
	    		//'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=news/news&news_id=' . $result['news_id']),
				'href_comment'  => $this->url->link('news/news', 'news_id=' . $result['news_id'] . '#comment_area'),
				//'href_comment'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=news/news&news_id=' . $result['news_id'] . '#comment_area'),
				'news_comment_count' =>$this->model_catalog_news->getTotalCommentsByNewsId($result['news_id'])
      		);
    	}
		// News All
      		
      		// Pagination All News start      		
				
				$news_total = $this->model_catalog_news->getTotalNews();
			
				$pagination = new Pagination();
				$pagination->total = $news_total;
				$pagination->page = $page;
				$pagination->limit = $this->config->get('news_setting_news_per_page'); 
				$pagination->text = $this->language->get('text_pagination');
				$pagination->url = HTTP_SERVER . 'index.php?route=news/news&page={page}';
			
				$this->data['pagination'] = $pagination->render();
      		// Pagination All News end

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/news_all.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/news/news_all.tpl';
			} else {
				$this->template = 'default/template/news/news_all.tpl';
			}
			
			$this->children = array(
				'common/column_right',
				'common/footer',
				'common/column_left',
				'common/header'
			);
		
	  		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    	}
  	}
  	
  	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
		
}
?>